//
//  PlistTool.m
//  PlistTool
//
//  Created by 王恕 on 16/1/14.
//  Copyright © 2016年 王恕. All rights reserved.
//

#import "PlistTool.h"

@implementation PlistTool

- (NSString *)getFileName{
    //获取沙盒目录
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *plistPath = [paths objectAtIndex:0];
    //得到完整的文件名
    NSString *fileName =[plistPath stringByAppendingPathComponent:@"data.plist"];
    NSLog(@"%@",fileName);
    
    return  fileName;
    
}

//使用懒加载的方式得到数据
-(NSMutableArray *) getAllData{
    
    //将上文中创建的Plist文件中的数据存储到数组中
    NSMutableArray *getArray = [[NSMutableArray alloc] initWithContentsOfFile:[self getFileName]];
    if (getArray == nil) {
        getArray = [NSMutableArray array];
        return getArray;
    }
    else{
        return getArray;
    }
}

//增加Plist文件中的数据
-(void) addNewDataWith:(NSString *) data{
    
    NSMutableArray *tmpArray = [NSMutableArray array];
    
    tmpArray = [self getAllData];
    
    [tmpArray addObject:data];
    
    //数据存入Plist文件
    [tmpArray writeToFile:[self getFileName] atomically:YES];
}

//删除Plist文件中的数据
- (void) deleteDataWith:(NSString *) data{
    
    NSMutableArray *tmpArray = [NSMutableArray array];
    
    tmpArray = [self getAllData];
    
    [tmpArray removeObject:data];
    
     //数据存入Plist文件
    [tmpArray writeToFile:[self getFileName] atomically:YES];
}


@end
